//
// Created by 许欣鑫 on 25-3-25.
//

#ifndef SOLUTION_1_2_H
#define SOLUTION_1_2_H
#include <vector>
#include <__algorithm/ranges_sort.h>
using namespace std;
constexpr int MOD = 1000000007;
class Solution_1_2 {
public:
    //打家劫舍 https://leetcode.cn/problems/house-robber/
    template<typename T>
    int rob(vector<T> &nums) {
        int size = nums.size();
        vector<T> dp(size + 1, 0);
        dp[1] = nums[0];
        for (int i = 2; i < size + 1; i++) {
            dp[i] = max(dp[i - 1], dp[i - 2] + nums[i - 1]);
        }
        return dp[size];
    }

    //删除并获得点数 https://leetcode.cn/problems/delete-and-earn/description/
    //实际上和打家劫舍相同 先还原到打家劫舍
    int deleteAndEarn(vector<int> &nums) {
        ranges::sort(nums);
        vector<int> nums_rob;
        int temp = nums[0];
        for (int i = 1; i < nums.size(); i++) {
            if (nums[i] != nums[i - 1]) {
                nums_rob.push_back(temp);
                if (nums[i] - nums[i - 1] > 1) {
                    nums_rob.push_back(0);
                }
                temp = nums[i];
            } else {
                temp += nums[i];
            }
        }
        nums_rob.push_back(temp);
        for (auto &i: nums_rob) {
            cout << i << endl;
        }
        return rob(nums_rob);
    }

    //统计放置房子的方式数 https://leetcode.cn/problems/count-number-of-ways-to-place-houses/description/
    int countHousePlacements(int n) {
        vector<long> dp(n + 1, 0);
        dp[0] = 1L;
        dp[1] = 2L;
        for (int i = 2; i < n + 1; ++i) {
            dp[i] = (dp[i - 1] + dp[i - 2]) % MOD;
        }
        return (dp[n] * dp[n]) % MOD;
    }

    //打家劫舍2 https://leetcode.cn/problems/house-robber-ii/description/
    //其实就是0和size位置互斥,进行两次计算,取其中多的那一个
    int rob_2(vector<int> &nums) {
        int n = nums.size();
        vector<int> dp1(n, 0);
        vector<int> dp2(n, 0);
        if (n == 1) return nums[0];
        if (n == 0) return 0;
        dp1[0] = nums[0];
        dp1[1] = nums[0];
        dp2[1] = nums[1];
        for (int i = 2; i < n; i++) {
            dp1[i] = max(dp1[i - 2] + nums[i], dp1[i - 1]);
            dp2[i] = max(dp2[i - 2] + nums[i], dp2[i - 1]);
        }
        return max(dp1[n - 2], dp2[n - 1]);
    }

    //施咒的最大伤害 https://leetcode.cn/problems/maximum-total-damage-with-spell-casting/description/
    //打家劫舍pro
    long long maximumTotalDamage(vector<int> &power) {
        vector<pair<int, long long> > remap;
        //第一个位置是为了让if (remap[i].first - remap[j].first > 2)能够break
        int n = power.size();
        sort(power.begin(), power.end());

        for (int i = 0; i < n; i++)
            if (remap.back().first != power[i])
                remap.emplace_back(power[i], power[i]);
            else
                remap.back().second += power[i];

        int nn = remap.size() + 1;
        long long dp[nn];
        memset(dp, 0, sizeof dp);
        for (int i = 1; i < nn; i++) {
            dp[i] = dp[i - 1];
            for (int j = i - 1; j >= 0; j--)
                if (remap[i].first - remap[j].first > 2) {
                    dp[i] = max(dp[i], dp[j] + remap[i].second);
                    break;
                }
        }

        return dp[nn - 1];
    }

    //解决智力问题 https://leetcode.cn/problems/solving-questions-with-brainpower/description/
    long long mostPoints(vector<vector<int> > &questions) {
        const int N = questions.size();
        vector<long long> dp(N + 1);
        for (int i = 0; i < N; i++) {
            dp[i + 1] = max(dp[i + 1], dp[i]);
            const int iNew = min(N - 1, i + questions[i][1]);
            dp[iNew + 1] = max(dp[iNew + 1], dp[i] + questions[i][0]);
        }
        return dp.back();
    }
};
#endif //SOLUTION_1_2_H
